package com.cloud.system.controller;

import com.cloud.common.constants.GlobalConstants;
import com.cloud.common.domain.Result;
import com.cloud.common.enums.CheckType;
import com.cloud.common.enums.MenuType;
import com.cloud.entity.SysMenu;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.user_info.service.ISysMenuService;
import com.cloud.user_info.service.impl.SysPermissionService;
import com.cloud.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 *
 * @author yzj
 */
@Tag(name = "菜单信息")
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Operation(summary = "获取菜单路由")
    @GetMapping("selectRouters")
    public Result getRouters() {
        Long userId = SecurityUtils.getUserId();
        String sysCode = SecurityUtils.getSysCode();
        List<SysMenu> menuList = menuService.selectMenuTreeById(sysCode, userId);
        return Result.success("获取菜单路由成功", menuService.buildMenus(menuList));
    }

    @Operation(summary = "查询菜单列表")
    @GetMapping("/list")
    public Result list(SysMenu menu) {
        menu.setUserId(SecurityUtils.getUserId());
        menu.setSysCode(SecurityUtils.getSysCode());
        return Result.success("查询成功", menuService.selectMenuList(menu));
    }

    @Operation(summary = "查询菜单详情")
    @Parameter(name = "menuId", description = "菜单ID")
    @RequestPermissions("system:menu:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long menuId) {
        return Result.success("查询详情成功", menuService.selectMenuById(menuId));
    }

    @Operation(summary = "查询菜单下拉树列表")
    @GetMapping("/treeSelect")
    public Result treeSelect(SysMenu menu) {
        menu.setUserId(SecurityUtils.getUserId());
        menu.setSysCode(SecurityUtils.getSysCode());
        return Result.success("查询成功", menuService.buildMenuTreeSelect(menu));
    }

    @Operation(summary = "查询角色菜单树列表")
    @Parameter(name = "roleId", description = "角色ID")
    @GetMapping("/roleMenuTreeSelect")
    public Result roleMenuTreeSelect(Long roleId) {
        Map<String, Object> map = new HashMap<>();
        map.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        map.put("menuList", menuService.buildMenuTreeSelect(new SysMenu()));
        return Result.success("查询成功", map);
    }

    @Operation(summary = "新增菜单信息")
    @Logs(title = "新增菜单信息", businessType = BusinessType.INSERT)
    @RequestPermissions("system:menu:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysMenu menu) {
        if (CheckType.NOT_UNIQUE.getCode().equals(menuService.checkMenuNameUnique(menu))) {
            return Result.error("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
        } else if (MenuType.YES_FRAME.getCode().equals(menu.getIsFrame())
                && !StringUtils.startsWithAny(menu.getPath(), GlobalConstants.HTTP, GlobalConstants.HTTPS)) {
            return Result.error("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
        }
        menu.setSysCode(SecurityUtils.getSysCode());
        return Result.success("操作成功", menuService.insertMenu(menu));
    }

    @Operation(summary = "修改菜单信息")
    @Logs(title = "修改菜单信息", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:menu:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysMenu menu) {
        if (CheckType.NOT_UNIQUE.getCode().equals(menuService.checkMenuNameUnique(menu))) {
            return Result.error("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
        } else if (MenuType.YES_FRAME.getCode().equals(menu.getIsFrame())
                && !StringUtils.startsWithAny(menu.getPath(), GlobalConstants.HTTP, GlobalConstants.HTTPS)) {
            return Result.error("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
        } else if (menu.getMenuId().equals(menu.getParentId())) {
            return Result.error("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己");
        }
        menu.setSysCode(SecurityUtils.getSysCode());
        return Result.success("操作成功", menuService.updateMenu(menu));
    }

    @Operation(summary = "删除菜单信息")
    @Parameter(name = "menuId", description = "菜单ID")
    @Logs(title = "删除菜单信息", businessType = BusinessType.DELETE)
    @RequestPermissions("system:menu:remove")
    @PostMapping("/remove")
    public Result remove(Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return Result.error("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return Result.error("菜单角色已分配,不允许删除");
        }
        return menuService.deleteMenuById(menuId);
    }

    @Operation(summary = "获取账户权限列表")
    @GetMapping("/selectMenuAuth")
    public Result selectMenuAuth(String sysCode) {
        Long userId = SecurityUtils.getUserId();
        return Result.success("查询成功", permissionService.getMenuPermission(sysCode, userId));
    }

}